package org.lonetree.tc.webapp.actions;

import java.util.Iterator;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.lonetree.tc.core.Book;
import org.lonetree.tc.core.BookComment;
import org.lonetree.tc.core.Book.Isbn;
import org.lonetree.tc.core.dao.BookDAO;
import org.lonetree.tc.core.dao.DAOFactory;

public class UpdateBookComment extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		
		try{
			if (request.getMethod().toLowerCase().equals("get"))
			{
			String isbn=request.getParameter("isbn");
			DAOFactory fac = DAOFactory.getDAOFactory(DAOFactory.FactoryImplementation.DB_DAO_FACTORY);
			BookDAO bdao = fac.getBookDAO();
			Book book=bdao.getBook(new Isbn(isbn));
			request.setAttribute("book", book);
			Set<BookComment> comments=book.getComments();
			Iterator<BookComment> itr=comments.iterator();
			while (itr.hasNext())
			{
				BookComment comment=itr.next();
				if (comment.getBookCommentId().getBookCommentIdLong().intValue()
						==Integer.parseInt(request.getParameter("comment_id")))
					request.setAttribute("comment", comment);
			}
			
			return mapping.findForward("success");
			}
			
			if (request.getMethod().toLowerCase().equals("post"))
			{
				request.setAttribute("isbn", request.getParameter("isbn"));
	            request.setAttribute("comment_id", request.getParameter("bookCommentId"));
				Connection connection=null;
		            Class.forName("com.mysql.jdbc.Driver");
		            connection =DriverManager.getConnection
		                    ("jdbc:mysql://localhost:3306/tc","tc","textbookcrossroads");
		            
		            //String id=request.getParameter("bookCommentId");
		            int id=Integer.parseInt(request.getParameter("bookCommentId"));
		            String comment=request.getParameter("comment");
	
		            //String flag=request.getParameter("flag");
		            int flag=Integer.parseInt(request.getParameter("flag"));
		            /*String query=
		            	"update textbook_comments set comments='"+comment+"' , is_flagged='"+flag+"' " +
		            			"where id='"+id+"'";
		            */
		            String query="update textbook_comments set comments='"+comment+"', is_flagged='"+flag+"' where id='"+id+"'";
		            Statement statement=connection.createStatement();
		            statement.executeUpdate(query);
		            connection.close();
		            return mapping.findForward("success_post");

		        }  
			
			return mapping.findForward("failure");
			
		}
		catch (Exception e)
		{	
			return mapping.findForward("failure");
		}
	}

}
